package org.example.anonymous;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * 比较对象
 */
public class Test02 {


    public static class UserPO {

        public UserPO(Long id, String username, Integer age) {
            this.id = id;
            this.username = username;
            this.age = age;
        }

        private Long id;

        //根据年龄 年龄越大的越靠前展示
        private String username;

        private Integer age;


        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "UserPO{" +
                    "id=" + id +
                    ", username='" + username + '\'' +
                    ", age=" + age +
                    '}';
        }

    }

    public static class Employee {

        private Integer id;
        private String name;
        private Integer age;
        private Double salary;

        public Employee(Integer id, String name, Integer age, Double salary) {
            this.id = id;
            this.name = name;
            this.age = age;
            this.salary = salary;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public Double getSalary() {
            return salary;
        }

        public void setSalary(Double salary) {
            this.salary = salary;
        }

        @Override
        public String toString() {
            return "Employee{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    ", salary=" + salary +
                    '}';
        }
    }


    public static void main(String[] args) {

        List<UserPO> userList = new ArrayList<>();

        //根据年龄倒序排序
        userList.add(new UserPO(1L, "zhangsan", 23));
        userList.add(new UserPO(2L, "lisi", 24));
        userList.add(new UserPO(3L, "wangwu", 20));
        userList.sort((o1, o2) -> {
            //lambda表达式的写法
            System.out.println("正在比较");
            return o2.getAge().compareTo(o1.getAge());
        });


        System.out.println(userList);


        List<Employee> emps = Arrays.asList(
                new Employee(101, "Z3", 19, 9999.99),
                new Employee(102, "L4", 20, 7777.77),
                new Employee(103, "W5", 35, 6666.66),
                new Employee(104, "Tom", 20, 1111.11),
                new Employee(105, "Jerry", 60, 4444.44)
        );

        //通过定制排序 比较两个 Employee (先按照年龄比升序，年龄相同按照姓名比自然顺序)，
        // 使用 Lambda 表达式作为参数传递

        //jvm会进行类型推断，lambda写法
        emps.sort((o1, o2) -> {
                if (o1.getAge().compareTo(o2.getAge()) > 0) {
                    return -1;
                } else if (o1.getAge().compareTo(o2.getAge()) == 0) {
                    //年龄相等,按照姓名排序（自然顺序） Jack Tom
                    return o1.getName().compareTo(o2.getName());
                } else {
                    return 1;
                }
        });

        System.out.println(emps);


    }
}
